package com.xl.rentkeeper.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xl.fuse.protos.account.User;
import com.xl.rentkeeper.App;
import com.xl.rentkeeper.R;
import com.xl.rentkeeper.act.LogUploadActivity;
import com.xl.rentkeeper.act.Push;
import com.xl.rentkeeper.act.setting.ChangePwdAct;
import com.xl.rentkeeper.business.UserLogic;
import com.xl.rentkeeper.log.QLog;
import com.xl.rentkeeper.net.NetEngine;
import com.xl.rentkeeper.web.WebEntry;

/**
 * Created by Administrator on 2015/7/21.
 * 设置的界面
 */
public class SettingFragment extends BaseFragment implements View.OnClickListener {
    private TextView mTVVersions;
    private TextView mTVExit;
    private TextView mTVPhone;
    private LinearLayout mLLTestBtn;
    private RelativeLayout mRLTVersion;
    private RelativeLayout mRLTLog;
    private RelativeLayout mRLPhone;
    private long lastClickVerTime = 0;
    private int clickCount =0;

    @Override
    protected View initView() {
        View view = View.inflate(mActivity, R.layout.fragment_setting, null);

        RelativeLayout mRLSetPwd = (RelativeLayout) view.findViewById(R.id.rl_setPwd);
        RelativeLayout mRLVersion = (RelativeLayout) view.findViewById(R.id.rl_versions);
        RelativeLayout mRLTLog = (RelativeLayout) view.findViewById(R.id.rl_log);
        mRLTLog.setOnClickListener(this);
        mTVVersions = (TextView) view.findViewById(R.id.tv_versions);
        mTVExit = (TextView) view.findViewById(R.id.tv_exit);
        mTVPhone = (TextView) view.findViewById(R.id.tv_phone);
        mLLTestBtn = (LinearLayout) view.findViewById(R.id.ll_testbtn);
        mRLTVersion = (RelativeLayout) view.findViewById(R.id.rl_versions);
        mRLPhone = (RelativeLayout) view.findViewById(R.id.rl_phone);
        mRLTVersion.setOnClickListener(this);

        mRLSetPwd.setOnClickListener(this);
        mTVExit.setOnClickListener(this);

        view.findViewById(R.id.bt_test).setOnClickListener(this);
        view.findViewById(R.id.bt_setip).setOnClickListener(this);
        view.findViewById(R.id.webview).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent in = new Intent(getActivity(), WebEntry.class);
                startActivity(in);
            }
        });

        return view;
    }

    @Override
    protected void initData() {

        //获取手机版本号
        try {
            PackageManager manager = mActivity.getPackageManager();
            PackageInfo info = manager.getPackageInfo(mActivity.getPackageName(), 0);

            int versionCode = info.versionCode;
            String versionName = info.versionName;
            mTVVersions.setText(versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        //设置手机号码
        User user = UserLogic.getInstance().getUser();
        if (TextUtils.isEmpty(user.phone )){
            mTVPhone.setText("未填写");
        }else {
            mTVPhone.setText(user.phone);
        }

    }

    @Override
    public void onEvent(String event, boolean ret, String errInfo,Object[] data) {

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.rl_setPwd:
                Intent intent = new Intent(mActivity, ChangePwdAct.class);
                startActivity(intent);
                break;
            case R.id.tv_exit:
                UserLogic.getInstance().logoutAndStart("ui");
                break;
            case R.id.bt_test:
                intent = new Intent(mActivity, Push.class);
                startActivity(intent);
                mActivity.overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);
                break;
            case R.id.rl_log:
                intent = new Intent(mActivity, LogUploadActivity.class);
                startActivity(intent);
                mActivity.overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);
                break;
            case R.id.bt_setip:
                setIp();
                break;
            case R.id.rl_versions:
                if(System.currentTimeMillis()-lastClickVerTime<=2000){
                    clickCount++;
                }
                lastClickVerTime =System.currentTimeMillis();
                if(clickCount>=6){
                    if(mLLTestBtn.getVisibility() == View.VISIBLE){
                        mLLTestBtn.setVisibility(View.INVISIBLE);
                    }else{
                        mLLTestBtn.setVisibility(View.VISIBLE);
                    }
                    clickCount =0;
                }
                break;
        }
    }

    private void setIp(){
        AlertDialog.Builder b = new AlertDialog.Builder(mActivity);
        final EditText et = new EditText(mActivity);
        et.setText(NetEngine.getInstance().getIp());
        b.setView(et).setNegativeButton("取消",null).setPositiveButton("保存", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if(et.getText().toString().trim().equals("")){
                    App.showToast("IP格式不对");
                }else{
                    NetEngine.getInstance().setIp(et.getText().toString().trim());
                }
            }
        }).create().show();
    }

}
